<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
            <el-form-item label="评价商品" prop="pro_id">
					<el-select v-model="form.pro_id"  filterable style="width: 100%" @visible-change="getPro">
						<el-option v-for="item in pro" :key="item.gid" :label="item.title" :value="item.gid"/>
					</el-select>
			</el-form-item>
			<el-form-item label="评价内容" prop="txt">
				<el-input v-model="form.txt" clearable></el-input>
			</el-form-item>
			<el-form-item label="星级" prop="xing">
				  <el-rate v-model="form.xing"/>
			</el-form-item>
			<el-form-item label="用户id" prop="user_id">
				<el-input v-model="form.user_id" clearable type="number"></el-input>
			</el-form-item>
            <el-form-item label="时间" prop="time">
                <el-date-picker
               
                    value-format="yyyy-MM-dd HH:mm:ss"
                    type="datetime"
                    placeholder="请选择时间"
                />
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增评价',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {},
				pro:{},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入名称'}
					],
				}
			}
		},
		mounted() {

		},
		methods: {
			async getPro(){
				if(this.pro.length > 0) return
				var reqData = {'act':'xuan'}
				var res = await this.$API.system.goods.list.get(reqData);
				if(res.code == 200){
					this.pro = res.data.rows
				}
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.universal.handle.post(this.form,'/goods/evaluate_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form = data
			}
		}
	}
</script>

<style>
</style>
